package com.naihe.anderson.sms.broadcastReceiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;

import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.constants.NhSmsConstants;
import com.naihe.anderson.sms.entity.Nhsms;
import com.naihe.anderson.sms.manager.NhSmsMgr;

public class SendFilterSmsReceiver extends BroadcastReceiver {

	public static final String ACTION_SMS_SEND = "com.naihe.anderson.sms.send";

	@Override
	public void onReceive(Context context, Intent intent) {
		String actionName = intent.getAction();
		int resultCode = getResultCode();
		if (actionName.equals(ACTION_SMS_SEND)) {
			
			Bundle bundle = intent.getExtras();
			long nhsmsIntentId = 0;
			String address = "";
            if (bundle != null) { 
            	nhsmsIntentId = bundle.getLong(NhSmsConstants.NHSMS_INTENT_ID);
            	address = bundle.getString(NhSmsConstants.NHSMS_ADDRESS_INTENT);
            }
            
            NhSmsApp nhSmsApp = (NhSmsApp) context.getApplicationContext();
            NhSmsMgr nhSmsMgr = nhSmsApp.getNhSmsMgr();
            
			switch (resultCode) {
			case Activity.RESULT_OK:
				//("\n[Send]SMS Send:Successed!");
				nhSmsMgr.updateNhSmsStatus(Nhsms.MESSAGE_TYPE_SENT, 0, Nhsms.STATUS_COMPLETE, nhsmsIntentId, address,"SendFilterSmsReceiver");
				break;
			case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
				//("\n[Send]SMS Send:RESULT_ERROR_GENERIC_FAILURE!");
			case SmsManager.RESULT_ERROR_NO_SERVICE:
				//("\n[Send]SMS Send:RESULT_ERROR_NO_SERVICE!");
			case SmsManager.RESULT_ERROR_NULL_PDU:
				//("\n[Send]SMS Send:RESULT_ERROR_NULL_PDU!");
			case SmsManager.RESULT_ERROR_RADIO_OFF:
				//("\n[Send]SMS Delivery:RESULT_ERROR_RADIO_OFF!");
				nhSmsMgr.updateNhSmsStatus(Nhsms.MESSAGE_TYPE_FAILED, resultCode, Nhsms.STATUS_FAILED, nhsmsIntentId, address, "SendFilterSmsReceiver");
				break;
			default:
				break;
			}
		}

	}

}
